package com.scm.controller;

/**
 * @author CHL
 * @ClassName: ScmuserController
 * @Description: TODO
 * @date 2022/3/8 14:33
 * @Version 1.0
 */
import com.github.pagehelper.PageInfo;
import com.scm.dao.ScmuserMapper;
import com.scm.dao.UserModelMapper;
import com.scm.entity.Scmuser;
import com.scm.entity.UserModel;
import com.scm.entity.response.ResponseMessage;
import com.scm.service.IUserService;
import com.scm.util.DateUtil;
import com.scm.util.StringUtil;
import java.io.PrintStream;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/main/system"})
public class ScmuserController
{
    @Autowired
    private ScmuserMapper scmuserMapper;
    @Autowired
    private IUserService userService;
    @Autowired
    private UserModelMapper userModelMapper;
    private ResponseMessage rm;

    public boolean checkUser(Scmuser user, String[] modelcodes)
    {
        if (StringUtil.isEmpty(user))
        {
            this.rm.setMessage("用户信息不能为空");
            return false;
        }
        String account = user.getAccount();
        if (StringUtil.isEmpty(account))
        {
            this.rm.setMessage("帐号不能为空");
            return false;
        }
        String password = user.getPassWord();
        if (StringUtil.isEmpty(password))
        {
            this.rm.setMessage("密码不能为空");
            return false;
        }
        String name = user.getName();
        if (StringUtil.isEmpty(name))
        {
            this.rm.setMessage("姓名不能为空");
            return false;
        }
        String createDate = user.getCreateDate();
        if (StringUtil.isEmpty(createDate))
        {
            this.rm.setMessage("添加日期不能为空");
            return false;
        }
        if (!DateUtil.isValid(createDate))
        {
            this.rm.setMessage("日期格式不正确");
            return false;
        }
        Integer status = user.getStatus();
        if (StringUtil.isEmpty(status))
        {
            this.rm.setMessage("锁定状态不能为空");
            return false;
        }
        if (!status.toString().matches("[01]"))
        {
            this.rm.setMessage("锁定状态必须为数字");
            return false;
        }
        if ((modelcodes == null) || (modelcodes.length == 0))
        {
            this.rm.setMessage("权限不能为空");
            return false;
        }
        return true;
    }

    public boolean checkUpdateUser(Scmuser user, String[] modelcodes)
    {
        if (StringUtil.isEmpty(user))
        {
            this.rm.setMessage("用户信息不能为空");
            return false;
        }
        String account = user.getAccount();
        if (StringUtil.isEmpty(account))
        {
            this.rm.setMessage("账户不能为空");
            return false;
        }
        String password = user.getPassWord();
        if (StringUtil.isEmpty(password))
        {
            this.rm.setMessage("密码不能为空");
            return false;
        }
        String name = user.getName();
        if (StringUtil.isEmpty(name))
        {
            this.rm.setMessage("姓名不能为空");
            return false;
        }
        Integer status = user.getStatus();
        if (StringUtil.isEmpty(status))
        {
            this.rm.setMessage("锁定状态不能为空");
            return false;
        }
        if (!status.toString().matches("[01]"))
        {
            this.rm.setMessage("锁定状态必须为数字");
            return false;
        }
        if ((modelcodes == null) || (modelcodes.length == 0))
        {
            this.rm.setMessage("权限不能为空");
            return false;
        }
        return true;
    }

    @RequestMapping({"/user/add"})
    public ResponseMessage scmuserAdd(Scmuser user, String[] modelcodes)
    {
        System.out.println(modelcodes);
        this.rm = new ResponseMessage();
        boolean flag = checkUser(user, modelcodes);
        if (!flag)
        {
            this.rm.setCode(Integer.valueOf(3));
            return this.rm;
        }
        Scmuser u = this.scmuserMapper.selectByAccount(user.getAccount());
        if (u != null)
        {
            this.rm.setCode(Integer.valueOf(4));
            this.rm.setMessage("帐号" + user.getAccount() + "被注册了");
            return this.rm;
        }
        this.userService.save(user, modelcodes);
        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("添加成功");
        return this.rm;
    }

    @RequestMapping({"/user/delete"})
    public ResponseMessage delete(String account, Integer page, HttpServletRequest req)
    {
        this.rm = new ResponseMessage();
        if (account == null)
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("缺少参数");
        }
        else
        {
            String user = (String)req.getAttribute("login");
            if (account.equals(user))
            {
                this.rm.setCode(Integer.valueOf(4));
                this.rm.setMessage("当前用户正使用，无法删除");
                return this.rm;
            }
            this.userService.delete(account);
            this.rm.setCode(Integer.valueOf(2));
            this.rm.setMessage("删除成功");
            this.rm.setData(this.userService.showPage(page));
        }
        return this.rm;
    }

    @RequestMapping({"/user/update"})
    public ResponseMessage update(Scmuser user, String[] modelcodes)
    {
        this.rm = new ResponseMessage();
        boolean flag = checkUpdateUser(user, modelcodes);
        if (!flag) {
            return this.rm;
        }
        this.userService.update(user, modelcodes);

        this.rm.setCode(Integer.valueOf(2));
        this.rm.setMessage("修改用户成功");

        return this.rm;
    }

    @RequestMapping({"/user/show"})
    public PageInfo<Scmuser> select(Integer page)
    {
        PageInfo<Scmuser> info = this.userService.showPage(page);

        return info;
    }

    @RequestMapping({"/user/model"})
    public ResponseMessage showUserModels(String account)
    {
        this.rm = new ResponseMessage();
        if (account == null)
        {
            this.rm.setCode(Integer.valueOf(3));
            this.rm.setMessage("缺少入参");
        }
        else
        {
            List<UserModel> userModels = this.userModelMapper.selectUserModel(account);
            this.rm.setCode(Integer.valueOf(2));
            this.rm.setMessage("成功获取用户权限");
            this.rm.setData(userModels);
        }
        return this.rm;
    }
}

